import threading
import time
import codecs
from requests_html import HTMLSession
import pyperclip

'''
pyperclip粘贴小说网站
改HTMLSession爬取，
适配三七小说网
'''

begin = time.perf_counter()

#多线程类
class myTread(threading.Thread):
    def __init__(self,threadID,name,st):
        threading.Thread.__init__ (self)
        self.threadID = threadID
        self.name = name
        self.st = st
    def run(self):
        print('start ',str(self.name))
        threadget(self.st)
        print('end ',str(self.name))

txtcontent = {} #存储小说所有内容

#获取页面html源码
def getpage(url):
    page = session.get(url).html
    page.encoding = 'utf-8'
    return page

chaptername = []  #存放小说章节名字
chapteraddress = []     #存放小说章节地址

#获取小说所有章节以及地址
def getchapter(list):
    a_s = list.find('a')
    for i, a in enumerate(a_s):
        href = a.attrs['href']
        chap = a.text
        chaptername.append(chap)
        # 处理.和空格
        # chap = chap.replace('.', '')
        # cp = re.sub('^\d+', '第'+str(i+1)+'章 ', chap)
        # cp = chap.sub('\d', '第'+str(i+1)+'章 ')

        chapteraddress.append(url + '/' + href)

#获取章节内容
def getdetail(html):
    content = ''
    text = html.find('div#content', first=True).text
    content = text
    return content

def threadget(st):
    # 线程数小于章节数组长度
    max = len(chaptername)
    #print('threadget函数',st,max)
    while st < max:
        url = str(chapteraddress[st])
        html = getpage(url)
        content = getdetail(html)
        txtcontent[st] = content
        print('下载完毕'+chaptername[st])
        st += thread_count

# 获取请求对象
session = HTMLSession()
url = pyperclip.paste()
# 往网站发送get请求
page = session.get(url)
page.encoding = 'utf-8'
list = page.html.find('div#list', first=True)
_name = page.html.find('div#info', first=True)
name = _name.find('h1', first=True).text
getchapter(list)

thread_list = []
thread_count = int(input('请输入需要开的线程数'))
# 开启线程数
for id in range(thread_count):
    thread1 = myTread(id,str(id),id)
    thread_list.append(thread1)
# 多线程开启
for t in thread_list:
    t.setDaemon(False)
    t.start()

for t in thread_list:
    t.join()
print('\n子线程运行完毕')
txtcontent1 = sorted(txtcontent)
file = codecs.open('C:/Users/ASUS/Desktop/'+name+'.txt','w','utf-8')  #小说存放在本地的地址
chaptercount = len (chaptername)

#写入文件中
for ch in range(chaptercount):
    title = '\n           第' + str (ch + 1) + '章  ' + str (chaptername[ch]) + '         \n\n'
    content = str(txtcontent[txtcontent1[ch]])
    file.write(title+content)
file.close()
end = time.perf_counter()
print('下载完毕，总耗时',end-begin,'秒')


